(function() {
    "use strict";

    if (!window.cl) {
        window.cl = {};
    }    

    window.cl.EyeProtective = {
        colorValue: 0,
        setColorValue: null
    };
    
    function setupWebViewJavascriptBridge(callback) {
        if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
        if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
        window.WVJBCallbacks = [callback];
        var WVJBIframe = document.createElement('iframe');
        WVJBIframe.style.display = 'none';
        WVJBIframe.src = 'https://__bridge_loaded__';
        document.documentElement.appendChild(WVJBIframe);
        setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
    }
    setupWebViewJavascriptBridge((bridge) => {
        var re = /function\s*(\w*)/i;
        function native_log(param,callback) {
            bridge.callHandler(re.exec(arguments.callee.toString())[1],param,callback);
        }
        bridge.registerHandler('setThemeStyle',(data,callback) => {
            var res = 0;
            var colorValue = data["colorValue"];
            var Lcolor = ""; //用于记录网页中获取的背景颜色
            var tcolor = "gray";
            var tLcolor = "";
            
            var color = "#333333"; //改变后的背景颜色
            
            if (colorValue == "乡土黄") {// "yellow"
                color = "#F6F4EC";
            } else if (colorValue == "豆沙绿") {// "green"
                color = "#CCE8CF";
            } else if (colorValue == "浅色灰") {// "grey"
                color = "#F2F2F2";
            } else if (colorValue == "淡橄榄") {// "olive"
                color = "#E1E6D7";
            } else if (colorValue == "黑夜") {// "black"
                color = "#333333";
            }
            
            try {
                Lcolor = document.defaultView.getComputedStyle(document.body, "").getPropertyValue("background-Color");
            } catch (e) {
                return;
            }
            FGrgb(Lcolor);
            
            if ((Gr > Gr1 && Gg > Gg1 && Gb > Gb1) || Lcolor == "transparent" || Gr === 0 && Gg === 0 && Gb === 0) //transparent表示透明
            {
                document.body.style.backgroundColor = color;
                document.body.style.webkitTextFillColor = tcolor;
            }
            
            changeAllElementsColor(color,tcolor);
            native_log("setThemeStyle:"+colorValue+"->"+color);
            fixAutoPage();
            callback(res);
        });
        
        
        //当网页的背景颜色的rgb值分别大于Gr1,Gg1,Gb1时此脚本将把颜色改成目标颜色color
        var Gr1 = 100; //RGB中的R值
        var Gg1 = 100; //RGB中的G值
        var Gb1 = 100; //RGB中的B值

        //**********以下代码用户无需修改***********//
        var Gr, Gg, Gb; //全局变量记录当前标签的rgb值,用于比较

        //以下函数用于分解获取的"rgb(255, 255, 255)"格式的rgb
        function FGrgb(Grgb) {
            Grgb = Grgb.replace(/[rgba\(\)]/g, '');
            var kaisi = Grgb.split(",");
            if (kaisi < 3) return;
            Gr = parseInt(kaisi[0]);
            Gg = parseInt(kaisi[1]);
            Gb = parseInt(kaisi[2]);
        }

        function changeAllElementsColor(color,tcolor) {
            var Lcolor = ""; //用于记录网页中获取的背景颜色
            //获取并修改所有标签的背景颜色
            var alltags = document.getElementsByTagName("*");
            var len = alltags.length;
            for (var i = 0; i < len; i++) {

                if (alltags[i].style.backgroundColor != color) {
                    Lcolor = document.defaultView.getComputedStyle(alltags[i], "").getPropertyValue("background-Color");
                    FGrgb(Lcolor);
                    if (Gr > Gr1 && Gg > Gg1 && Gb > Gb1) {
                        alltags[i].style.backgroundColor = color;
                    }
                }
                else if (alltags[i].style.webkitTextFillColor != tcolor) {
                    alltags[i].style.webkitTextFillColor = tcolor;
                }
            }
        }

        function fixAutoPage() {
            var _bodyHeight = document.body.clientHeight;
            // 创建观察者对象
            var observer = new window.MutationObserver(function(mutations) {
                if (mutations[0].addedNodes) {
                    if (document.body.clientHeight > _bodyHeight) {
                        setTimeout(function() {
                            changeAllElementsColor();
                        }, 200);

                        _bodyHeight = document.body.clientHeight;
                    }
                }
            });
            observer.observe(document, {
                childList: true,
                subtree: true
            });
        }

//        window.cl.EyeProtective.setColorValue = function (colorValue) {
//            if (colorValue === window.cl.EyeProtective.colorValue) {
//                return;
//            }
//
//            window.cl.EyeProtective.colorValue = colorValue;
//
//            var color = "#F6F4EC"; //改变后的背景颜色
//            var Lcolor = ""; //用于记录网页中获取的背景颜色
//
//            var tcolor = "gray";
//            var tLcolor = "";
//
//            //获取并修改body的背景颜色.
//            if (colorValue == 0) // "yellow"
//            {
//                color = "#F6F4EC";
//            }
//            else if (colorValue == 1) // "green"
//            {
//                color = "#CCE8CF";
//            }
//            else if (colorValue == 2) // "grey"
//            {
//                color = "#F2F2F2";
//            }
//            else if (colorValue == 3) // "olive"
//            {
//                color = "#E1E6D7";
//            }
//            else if (colorValue == 4) // "black"
//            {
//                color = "#333333";
//            }
//            try {
//                Lcolor = document.defaultView.getComputedStyle(document.body, "").getPropertyValue("background-Color");
//            } catch (e) {
//                return;
//            }
//
//            FGrgb(Lcolor);
//
//            if ((Gr > Gr1 && Gg > Gg1 && Gb > Gb1) || Lcolor == "transparent" || Gr === 0 && Gg === 0 && Gb === 0) //transparent表示透明
//            {
//                document.body.style.backgroundColor = color;
//                document.body.style.webkitTextFillColor = tcolor;
//            }
//
//            changeAllElementsColor(color,tcolor);
//
//            fixAutoPage();
//        };
    });
    
})();
